package test;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.HashSet;
import java.util.Set;
import engine.Character;
import engine.Level;
import engine.Player;

public class Mario extends Player {
    private final int SPEED_MULTIPLIER = 3;
    private final int CAN_JUMP = 0, JUMPING = 1, FALLING = 2;
    private final int JUMP_TIME = 30;
    
    private int hp;
    private Set<Integer> keyset;
    private int jumpTimer;
    private int jumpState;
    
    
    public Mario(int x, int y) {
        super();
        keyset = new HashSet<Integer>();
        changeSpeed(new Point(0,1));
        setPosition(new Point(x,y));
        hp = 10;
        jumpTimer = 0;
        jumpState = FALLING;
    }
    
    public void damage() {
        hp--;
    }

    @Override
    public Rectangle getBounds () {
        return new Rectangle((int)getPosition().getX(), (int)getPosition().getY(),50,50);
    }


    @Override
    public void paint (Graphics g, int offsetX, int offsetY) {
        g.setColor(Color.RED);
        g.fillRect((int)getPosition().getX()-offsetX,(int)getPosition().getY()-offsetY,50,50);
    }

    @Override
    public void keyPressed (KeyEvent k) {
        keyset.add(k.getKeyCode());
    }

    @Override
    public void keyReleased (KeyEvent k) {
        keyset.remove(k.getKeyCode());
    }
    
    public void endJump() {
        jumpTimer = 0;
        jumpState = CAN_JUMP;
    }
    
    public void startJump() {
        jumpState = JUMPING;
    }

    @Override
    public void updateState (Level l) {
        if (hp <= 0) {
            markForRemoval();
        }
        int xSpeed = 0, ySpeed = 0;
        if (keyset.contains(KeyEvent.VK_LEFT)) {
            xSpeed = -1 * SPEED_MULTIPLIER;
        }
        if (keyset.contains(KeyEvent.VK_RIGHT)) {
            xSpeed = 1 * SPEED_MULTIPLIER;
        }
        if (keyset.contains(KeyEvent.VK_SPACE)) {
            if (jumpState == CAN_JUMP) {
                jumpState = JUMPING;
            }
        }

        if (jumpState == JUMPING) {
            ySpeed = -1 * SPEED_MULTIPLIER;
            jumpTimer++;
        }
        if (jumpState == FALLING) {
            ySpeed = 1 * SPEED_MULTIPLIER;
        }
        if (jumpTimer > JUMP_TIME) {
            jumpState = FALLING;
        }
        
        changeSpeed(new Point(xSpeed, ySpeed));
        System.out.println(keyset);
    }
}
